<?php
namespace app\admin\controller;

use app\admin\model\Hospital as hosModel;
use app\admin\validate\HosValidate;
use tool\Log;

class Hospital extends Base
{
    public function __construct() {
        parent::__construct();
        $this->hosp = new \app\admin\model\Hospital();
        $this->assign('notes', $this->hosp->notes);
    }

    public function index()
    {
        if (request()->isAjax()) {
            $limit = input('param.limit');
            $keywords = input('param.keywords');
            $where = [];
            if (!empty($keywords)) {
                $where[] = ['title', 'like', '%' . $keywords . '%'];
            }

            $hos = new hosModel();
            $list = $hos->getHos($limit, $where);
            if (0 == $list['code']) {
                return json(['code' => 0, 'msg' => 'ok', 'count' => $list['data']->total(), 'data' => $list['data']->all()]);
            }
            return json(['code' => 0, 'msg' => 'ok', 'count' => 0, 'data' => []]);
        }
        return $this->fetch();
    }
    // 添加医院
    public function add()
    {
        if (request()->isPost()) {

            $param = input('post.');

            $validate = new HosValidate();
            if (!$validate->check($param)) {
                return json(['code' => -1, 'data' => '', 'msg' => $validate->getError()]);
            }

            $hos = new hosModel();
            $res = $hos->addHos($param);

            Log::write("添加医院：" . $param['hos_name']);

            return json($res);
        }

        return $this->fetch();
    }
    // 编辑医院
    public function edit()
    {
        if (request()->isPost()) {

            $param = input('post.');

            $validate = new WebsiteValidate();
            if (!$validate->scene('edit')->check($param)) {
                return json(['code' => -1, 'data' => '', 'msg' => $validate->getError()]);
            }

            $hos = new hosModel();
            $res = $hos->editHos($param);

            Log::write("编辑医院：" . $param['hos_name']);

            return json($res);
        }

        $id = input('param.id');
        $hos = new hosModel();

        $this->assign([
            'item' => $hos->getHosById($id)['data'],
        ]);

        return $this->fetch('edit');
    }

    /**
     * 删除医院
     * @return \think\response\Json
     */
    public function del()
    {
        if (request()->isAjax()) {
            $id = input('param.id');

            $hos = new hosModel();
            $res = $hos->delCate($id);

            Log::write("删除医院：" . $id);
            return json($res);
        }
    }
    /**
     * 医院缩略图
     */
    public function uploadPic() {
        // 获取表单上传文件
        $file = request()->file('file');
        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->move('./uploads/d/file/p/');
        if($info){
            // $path =  $info->getExtension();
            $path =  $info->getSavename();  // 输出 20160820\42a79759f284b767dfcb2a0197904287.jpg
            $pathArr = explode('\\', $path); // TODO win环境下是\  linux下是/
            $pathYear = substr($pathArr[0],0,4);
            $pathMonth = substr($pathArr[0],4,2);
            $pathDay = substr($pathArr[0],6,2);
            $realPath = '/uploads/d/file/p/'.$pathYear.'/'.$pathMonth.'-'.$pathDay.'/'.$pathArr[1]; //输出 /uploads/d/file/p/2016/08-20/42a79759f284b767dfcb2a0197904287.jpg
            // $realPath = '/uploads/d/file/p/'.$path;
            $realPath = str_replace('\\','/',$realPath);
            // 成功上传后 返回上传信息
            return reMsg('0',$realPath,'图片上传成功');
        }else{
            // 上传失败返回错误信息
            return reMsg('-1','','图片上传失败');
        }
    }

}